﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using YAWD.Data;

namespace YAWD.BL.Services
{
    class WallPaperServices
    {
        #region Include Folder
        public static void AddIncludeFolder(string folderPath)
        {
            if (GetIncludedFolder(folderPath) == 0)
                Dal.GetInstance.AddIncludeFolder(folderPath);
        }

        public static void DeleteIncludedFolder(string folderPath)
        {
            Dal.GetInstance.DeleteIncludeFolder(folderPath);
        }

        #endregion
        #region ExcludeWallpaper
        public static void addExcludeWallPaper(string filePath)
        {
            Dal.GetInstance.AddExcludeWallpaper(filePath);
        }
        public static int getExcludeWallPaper(string filePath)
        {
            return Dal.GetInstance.GetExcludeWallPaper(filePath);
        }
        #endregion

        private static int GetIncludedFolder(string folderPath)
        {
            return Dal.GetInstance.GetIncludeFolder(folderPath);
        }
        public static IList<string> GetIncludedFolders()
        {
            return Dal.GetInstance.GetIncludeFolders();
        }
        public static string GetRandomWallpaper()
        {
            ArrayList files = new ArrayList();
            IList<string> folders = GetIncludedFolders();

            if (folders==null || folders.Count == 0)
                throw new Exception("No Picure Folder Found." + Environment.NewLine +"Please go to Options -> Manage Selection Folders");

            foreach (string item in folders)
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(item);
                files.AddRange(directoryInfo.GetFiles("*.png", SearchOption.AllDirectories));
                files.AddRange(directoryInfo.GetFiles("*.jpg", SearchOption.AllDirectories));
                files.AddRange(directoryInfo.GetFiles("*.jpeg", SearchOption.AllDirectories));
            }

            if (files.Count > 0)
            {
                Random random = new Random();
                string filePath = ((FileInfo)files[random.Next(0, files.Count - 1)]).FullName;

                if (getExcludeWallPaper(filePath) == 0)
                    return filePath;
                else
                    return GetRandomWallpaper();
            }

            return string.Empty;
        }
    }
}
